package org.ccu.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.ccu.common.ConnectionManager;
import org.ccu.vo.Building;
import org.ccu.vo.Comment;
import org.ccu.vo.Reply;

public class ReplyDAO {
	private static Connection conn = ConnectionManager.getConnection();
	private static Statement st = null;
	private static ResultSet rs = null;
	
	public static void main(String[] a){
		
		List<Reply> bs =ReplyDAO.getReply(31);
		for(Reply r:bs){
			r.printer();
		}
	}
	public static boolean saveReply(int commentid, String username,
			String content) {

		try {
			st = conn.createStatement();
			String sql = "insert into reply (reply_id,user_id,reply_content,comment_id) values((select max(reply_id)+1 from reply),(select user_id from myu where user_name='"+username+"'),'"+content+"',"+commentid+")";
			System.out.println(sql);
			st.execute(sql);
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return true;

	}

	public static List<Reply> getReply(int commentid) {
		List<Reply> bs = new ArrayList<Reply>();
		try {
			
			st = conn.createStatement();
			String sql = "select a.user_name,a.user_id,b.comment_id,b.reply_content from myu a,(select comment_id,user_id,reply_content from reply where comment_id="+commentid+" order by reply_id) b where a.user_id=b.user_id";	
			System.out.println(sql);
			rs = st.executeQuery(sql);

			while (rs.next()) {
				Reply r=new Reply(rs.getInt("comment_id"), rs.getString("user_name"), rs.getString("reply_content"));
				bs.add(r);
			}
			return bs;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		} finally {
			Boolean b = ConnectionManager.close(st, rs);
			return bs;
		}//buildingid tiaozhuang

	}

}
